package com.iflyun.redis.controller;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.iflyun.redis.dao.InitRedisDao;

import com.iflyun.redis.dao.RedisHashDao;

import com.iflyun.redis.entity.Bus;


public class Test {
	

	private Test(){};

    private static ApplicationContext appContext = null;
    
    private final static Logger logger = LoggerFactory.getLogger(Test.class);

    private static String appFile = "classpath:spring-mvc.xml";

    public static synchronized  ApplicationContext getApplicationContext() {
        if (null == appContext) {
            appContext = new ClassPathXmlApplicationContext(appFile);
        }
        return appContext;
    }

    private static void loadAppContextFile() throws Exception {
        appContext = new ClassPathXmlApplicationContext(appFile);
        logger.info("成功加载Spring配置文件");
    }
    
    public static void main(String[] args) {
    	try {
			loadAppContextFile();
		} catch (Exception e) {
			logger.error("加载出错",e);
		}
    	
    	
    	RedisHashDao redisHashDao=(RedisHashDao) appContext.getBean("redisHashDao");
    	InitRedisDao initRedisDao=(InitRedisDao) appContext.getBean("initRedisDao");
    	 

    	/**
    	 * 模糊查询	
    	 */
    	 List<String> keys=redisHashDao.getLike("0017:1:*", 100);
    	 /**
    	  * 先查询缓存 没有则对数据库进行查询
    	  */

    	 if(keys.isEmpty()){
    		 List<Bus> list =initRedisDao.getList("","1");
        	 List<Bus> list1 =initRedisDao.getList("","2");
        	 for (Bus bus : list) {
        		 redisHashDao.addBus(bus, "1", 600);
    		}
        	 for (Bus bus : list1) {
        		 redisHashDao.addBus(bus, "2", 600);
    		}
        	 keys=redisHashDao.getLike("0016:1:*", 100);
        	
    	 }
    	 
    	 for (String key : keys) {
				logger.info(redisHashDao.getBus(key).getRealName());
		}
    	
    	/**
    	 * 单条数据30秒过期
    	 */
    	/*redisDao.addTtl(new Redis("key003","value003"),30);
    	System.out.println(redisDao.get("key001"));*/
    	
    	//保存单个
    	/*Redis redis=new Redis("key004","value004");
    	redisDao.add(redis);*/
    	
    	 /**
    	  * 模糊搜索
    	  */
    	/*List<String> list=redisDao.getLike("key*",10);
    	for (String string : list) {
			System.out.println(redisDao.get(string).getValue());
		}*/
    	
    	/**
    	 * 批量保存
    	 */
    	/*List<Redis> list=new ArrayList<>();
    	list.add(new Redis("key004","value004"));
    	list.add(new Redis("key005","value005"));
    	redisDao.add(list);*/
    	
    	/**
    	 * 删除记录
    	 */
//    	redisDao.delete("key005");
    	
    	/**
    	 * 批量删除	
    	 */
    	/*List<String> list=new ArrayList<>();
    	list.add("key004");
    	list.add("key003");
    	redisDao.delete(list);*/
    	
    	/**
    	 * 修改
    	 */
    	/*redisDao.update(new Redis("key002", "value02"));*/
    	
 	
    	//redisHashDao.add(new Redis("key004","value004"));
    	
    	//redisHashDao.getLike("key00*", 10);
    	
    	
    	logger.info("完成");
	}
}
